<?php 
/**
 * Barcode Controller Create By Norman
 * All This funcions belong to Barcode Generator 
 * There are 24 Actions At the Moment 
 * Click Link Below to Go to Barcode Generator 
 * @link http://192.168.1.124/barcode
 * @author Norman
 * @since The Barcode Generator Made on Year 2013
 */
class BarcodeController extends Zend_Controller_Action
{
	/**
	 * Index Funtion, only view is useful, contain all links of barcode function 
	 * @link http://192.168.1.124/barcode
	 */
    public function indexAction(){	
		// Empty Controller 
	}
	
	/**
	 * The following part is the management section , you may ADD and EDIT the folumla to create a barcode
	 * The Remove function did not provided becase it is not necessary, if you don't want to use that phone model , you may just ignore it 
	 * Unless it is too much , you do not need to remove them.
	 * The following functions are All finished, not more modification required at the moment 
	 */
	//------------------------------------ Management Section ----------------------------------------------------//
	/**
	 * Management of Mobile Phone Manufacture Function
	 * In Barcode Generator , Mobile Phone Brand / Manufacture is the first / part of the barcode 
	 * @link http://192.168.1.124//barcode/manage-manu
	 */
	public function manageManuAction(){
		
		$manu = new Model_DbTable_Barcodemanu();

		
		
		if($_POST){
			
			if(isset($_POST['sub_2']) and $_POST['sub_2']=="Add"){
				
				$manu->addManu(trim($_POST['id_add_manu']),trim($_POST['code_add_manu']),trim($_POST['des_add_manu']));
				
				
				echo "New Line ADD";
						
			}
			if(isset($_POST['sub_1']) and $_POST['sub_1']=="Modify"){
			
				
				
				foreach($_POST['row'] as $key => $value){
					
					//var_dump($value);
					
					$manu->updateManu($value["id"],$value["code"],$value["des"]);
				}				
				
				echo "Modification Saved, F5 Your Page!!";
			}			
				
		}

		$this->view->list = $manu->listManu();		
		
	}
	/**
	 * Manage Phone Model Function 
	 * Model is after the manufacture , different brand phone has different model of phone 
	 * Apple (manufacture) -> iphone 6 (Model)
	 */
	public function manageModelAction(){
		
		$manu = new Model_DbTable_Barcodemanu();
		$model = new Model_DbTable_Barcodemodel();
		
		$this->view->manulist = $manu->listManu();		
		$idManu = 1;
		
		if($_POST){
			
			//choose Manufacure	
			if(isset($_POST['sub_go']) and $_POST['sub_go']=="GO"){	

				$idManu = $_POST['id_manu_model'];
			}
			//modify
			if(isset($_POST['sub_1']) and $_POST['sub_1']=="Modify"){
					
			
			
				foreach($_POST['row'] as $key => $value){
						
					//var_dump($value);
						
					//$manu->updateManu($value["id"],$value["code"],$value["des"]);
					$model->updateModel($value["id"],$_POST["id_manu"],$value["code"],$value["des"],$value["short_code"]);
				}
			
			
				echo "Modification Saved, F5 Your Page!!";
			}
							
			
			//add
			
			if(isset($_POST['sub_2']) and $_POST['sub_2']=="Add"){
			
				//$manu->addManu(trim($_POST['id_add_manu']),trim($_POST['code_add_manu']),trim($_POST['des_add_manu']));
				$model->addmodel($_POST["id_add_manu"],trim($_POST['code_add_model']), trim($_POST['des_add_model']),trim($_POST['short_code_add_model']));
			
				echo "New Line ADD";
			
			}	
			
			
		}	

		
		//list Model By same Manufacture 
		$this->view->idManu = $idManu;
		$this->view->modellist = $model->listModelByManuID($idManu);
		
		
		
	}
	/**
	 * Manage Product Sub Type Function 
	 * There are only 10 of Main Type of Prouct , each of them has it's own Unique barcode generator 
	 * barcode generator is base on main type
	 * 
	 * 1.Case barcode 
	 * 2.Design Case / Defender case
	 * 3.Battery /Power Bank
	 * 4.Charger / Adaptor 
	 * 5.Data cable 
	 * 6.Hands Free 
	 * 7.Holder 
	 * 8.Screen Protector 
	 * 9.Other 
	 * 10.Stylus
	 * 
	 * for each Main Type , they have either Sub Type Or they have special Code Or both
	 * 
	 * 
	 */
	public function manageProductSubTypeAction(){
		
		$mainType = new Model_DbTable_Barcodeproducttype();
		$subType = new Model_DbTable_Barcodeproductsubtype();
		
		$this->view->mainlist = $mainType->listType();
		$idMainType = 1;
		
		if($_POST){
			//choose Main Product Type
			if(isset($_POST['sub_go']) and $_POST['sub_go']=="GO"){
			
				$idMainType = $_POST['id_main_type'];
			}			
			//Modify
			
			if(isset($_POST['sub_1']) and $_POST['sub_1']=="Modify"){
					
					
					
				foreach($_POST['row'] as $key => $value){
			
					//var_dump($value);
			
					//$manu->updateManu($value["id"],$value["code"],$value["des"]);
					$subType->updateSubType($value["id"],$_POST["id_main"],$value["code"],$value["des"]);
					//$model->updateModel($value["id"],$_POST["id_manu"],$value["code"],$value["des"]);
				}
					
					
				echo "Modification Saved, F5 Your Page!!";
			}			
			
			
			//Add
			
			if(isset($_POST['sub_2']) and $_POST['sub_2']=="Add"){
					
				$subType->addSubType(trim($_POST['id_add_main']),trim($_POST['code_add_sub_type']), trim($_POST['des_add_sub_type']));	
				echo "New Line ADD";
				$idMainType = trim($_POST['id_add_main']);
			}			
			
		}
		
		//
		
		$this->view->idMainType = $idMainType;
		$this->view->sublist = $subType->listSubTypeByMainTypeID($idMainType);
		
		
		
		
	}
	/**
	 * Management of special Code function 
	 * As explained in the SubType section 
	 * Only Some of the Main type have Subtype and some others has called Special Code 
	 * Both Subtype and Special Code has it's own Number record 
	 * The number in recorded in "barcode_counter" Table
	 * 
	 */
	public function manageSpecialcodeAction(){
		
		$mainType = new Model_DbTable_Barcodeproducttype();
		$spCode = new Model_DbTable_Barcode_Specialcode();
		
		$this->view->mainlist = $mainType->listType();
		
		//get special code 
		$idMainType = 2;
		
		if($_POST){
				
			//choose Product Type
			if(isset($_POST['sub_go']) and $_POST['sub_go']=="GO"){
		
				$idMainType = $_POST['id_maintype_sp_code'];
			}
			//modify
			if(isset($_POST['sub_1']) and $_POST['sub_1']=="Modify"){
					
					
					
				foreach($_POST['row'] as $key => $value){
		
					//var_dump($value);
		

					$spCode->updateSpecialCode($value["id"],$_POST["id_maintype"],$value["code"],$value["des"]);
		
				}
					
					
				echo "Modification Saved, F5 Your Page!!";
			}
				
				
			//add
				
			if(isset($_POST['sub_2']) and $_POST['sub_2']=="Add"){
					
				
				$spCode->addSpecialCode($_POST["id_add_main"],trim($_POST['code_add_sp_code']), trim($_POST['des_add_sp_code']));
	
					
				echo "New Line ADD";
				$idMainType = trim($_POST['id_add_main']);
					
			}
				
				
		}		
		
		$this->view->idMain = $idMainType;
		$this->view->spcodelist = $spCode->listSpecialCodeByProductTypeID($idMainType);
		
	
	}
	/**
	 * List and Manage all color code function 
	 * All Our Case has it's own color code 
	 * base color is from 1 to 11 
	 * other 12 to 19 means dark color 
	 * and 2x means light color 
	 * if you met color code other then this , it is patten or manual added
	 * please use this link @link http://www.w3schools.com/html/html_colornames.asp
	 * To give a apporate name of the color code 
	 */
	public function manageColorAction(){
		$color = new Model_DbTable_Barcodecolorcodes();
		if($_POST){
			$color->addColor($_POST['id_color'],$_POST['des_color'],$_POST['hex_code']);
			echo "Color Added";
		}
		
		$this->view->colorList = $color->listAllColors();
		//d($this->view->colorList);
	
	}
	
	//------------------------------------End of Management Section ----------------------------------------------------//
	
	/**
	 * The following Section is the section that user can actually create the barcode 
	 * all the barcode created will using save barcode store in the product_v1 table 
	 * then the csv file will be exort for today using create file function 
	 */
	
	//------------------------------------Begin of Create Barcode Secton  ----------------------------------------------------//
	/**
	 * Create Case barcode, Case barcode fumula is 
	 * Formula Case Type + Manu + Model - Color
	 */
	public function createBarcodeCaseAction(){
		
		
		//Initial case type 
		$mainType = 1;
		$subType = new Model_DbTable_Barcodeproductsubtype();		
		//manu
		$manu = new Model_DbTable_Barcodemanu();	
		//model 
		$model = new Model_DbTable_Barcodemodel();
		//color 
		$color = new Model_DbTable_Barcodecolorcodes();
		
		
		$modelList = array(array('code_model'=>"NA",'des_model'=>"NotAvailable"));
		
		
		
		// Initial Selections 
		
		$this->view->subtypeList = $subType->listSubTypeByMainTypeID(1);
		$this->view->manuList = $manu->listManu();
		$this->view->colorList = $color->listAllColors();
		
		//remember the choice 
		
		$this->view->subtypeChoice = "";
		$this->view->manuChoice = "";
		$this->view->colorChoice = "";
		$this->view->modelChoice = "";
		//barcode Des
		$this->view->barcodeCode= "";
		$this->view->barcodeDes ="";
		//initial array
		
		$arrBarcodeCode = array();
		$arrBarcodeDes = array();
		
		
		
		if($_POST){
			
			// create Model
			if(isset($_POST['sub_model']) and $_POST['sub_model']=="Model"){
				
				//Create Model List 
				$modelList = $model->listModelByManuCode(trim($_POST['manu']));
				//Remember the Choice
				$this->view->subtypeChoice = trim($_POST['sub_type']);
				$this->view->manuChoice = trim($_POST['manu']);
				if(isset($_POST['color'])){
				$this->view->colorChoice = $_POST['color'];
				}	
			}
			
			// Create Barcode 
			if(isset($_POST['sub_result']) and $_POST['sub_result']=="Result"){
				
				$modelList = $model->listModelByManuCode(trim($_POST['manu']));
				
				$this->view->subtypeChoice = trim($_POST['sub_type']);
				$this->view->manuChoice = trim($_POST['manu']);
				$this->view->colorChoice = $_POST['color'];
				$this->view->modelChoice = trim($_POST['model']);
				
				//create Barcode 
				
				$subtypeDes = $subType->getSubtypeDesByCode(trim($_POST['sub_type']));
				$manuDes = $manu->getManuDesByCode(trim($_POST['manu']));
				$modelDes = $model->getModelDesByCode(trim($_POST['model']));
				
				foreach($_POST['color'] as $key => $value){
					
					$colorDes = $color->getColorDes(trim($value));
					
					$barCodeBeforeTrim = trim($_POST['sub_type']).trim($_POST['manu']).trim($_POST['model']);
					$barCodeAfterTrim = $this->trimBarcodeColor($barCodeBeforeTrim,trim($_POST['manu']), trim($_POST['model']));
					$realBarcode = '';
					if($barCodeAfterTrim){
						$realBarcode = $barCodeAfterTrim."-".$value;
					}
					$arrBarcodeCode[] = $realBarcode;
					$arrBarcodeDes[] = "A0 ".$subtypeDes." ".$manuDes." ".$modelDes." ".$colorDes; 
				}
				
				$this->view->barcodeCode = $arrBarcodeCode;
				
				$this->view->barcodeDes = $arrBarcodeDes;
				
							
			}
			
			
		}
		//load model After Post 
		$this->view->modelList = $modelList;
		
		
		
	} 
	/**
	 * Design Case and Defender case are different from Normal case 
	 * Manu + Model +Type(HC/LC/SC)+Number +Color
	 */
	public function createBarcodeDesignCaseAction(){
		
		//Initial case type 
		$mainType = 10;
		$subType = new Model_DbTable_Barcodeproductsubtype();		
		//manu
		$manu = new Model_DbTable_Barcodemanu();	
		//model 
		$model = new Model_DbTable_Barcodemodel();
		//color 
		$color = new Model_DbTable_Barcodecolorcodes();
		$counters = new Model_DbTable_Barcode_Counter();
		
		$modelList = array(array('code_model'=>"NA",'des_model'=>"NotAvailable"));
		$number = "N/A";
		
		// Initial Selections 
		
		$this->view->subtypeList = $subType->listSubTypeByMainTypeID($mainType);
		$this->view->manuList = $manu->listManu();
		$this->view->colorList = $color->listAllColors();
		//remember the choice 
		
		$this->view->subtypeChoice = "";
		$this->view->manuChoice = "";
		$this->view->colorChoice = "";
		$this->view->modelChoice = "";
		//barcode Des
		$this->view->barcodeCode= "";
		$this->view->barcodeDes ="";
		//inital barcode array
		
		$arrBarcodeCode = array();
		$arrBarcodeDes = array();
		
		
		
		if($_POST){
			
			$modelList = $model->listModelByManuCode(trim($_POST['manu']));
			$this->view->subtypeChoice = trim($_POST['sub_type']);
			$this->view->manuChoice = trim($_POST['manu']);
			if(isset($_POST['color'])){
				$this->view->colorChoice = $_POST['color'];
			}
			
			// create Model
			if(isset($_POST['sub_model']) and $_POST['sub_model']=="Model"){
			}
			//create Number 
			if(isset($_POST['sub_number']) and $_POST['sub_number']=="Number"){

				$this->view->modelChoice = trim($_POST['model']);
				
				
				$ccounter = $counters->searchCounterByCode(trim($_POST['sub_type']),trim($_POST['model']));
				if($ccounter){
					$number=$ccounter['counter']+1;
				}
				else{
					$number=100;
				}
				d(trim($_POST['model']));
			
			}
			// Create Barcode 
			if(isset($_POST['sub_result']) and $_POST['sub_result']=="Result"){
				
				$this->view->modelChoice = trim($_POST['model']);
				$number = trim($_POST['number']);
				//create Barcode 
				
				$subtypeDes = $subType->getSubtypeDesByCode(trim($_POST['sub_type']));
				$manuDes = $manu->getManuDesByCode(trim($_POST['manu']));
				$modelDes = $model->getModelDesByCode(trim($_POST['model']));
				
				foreach($_POST['color'] as $key => $value){
						
					$colorDes = $color->getColorDes(trim($value));
					
					if(is_numeric($number)){
						$barCodeBeforeTrim = trim($_POST['manu']).trim($_POST['model']).trim($_POST['sub_type']).$number;
						$barCodeAfterTrim = $this->trimBarcodeColor($barCodeBeforeTrim,trim($_POST['manu']),trim($_POST['model']));
							$realCode = '';
						if($barCodeAfterTrim){
							$realCode = $barCodeAfterTrim."-".trim($value);
						}
						
						$arrBarcodeCode[]  = $realCode;
					}
					
					else{
						$barCodeBeforeTrim = trim($_POST['manu']).trim($_POST['model']).trim($_POST['sub_type']);
						$barCodeAfterTrim = $this->trimBarcodeColor($barCodeBeforeTrim,trim($_POST['manu']),trim($_POST['model']));
							$realCode = '';
						if($barCodeAfterTrim){
							$realCode = $barCodeAfterTrim."-".trim($value);
						}
						
						$arrBarcodeCode[]  = $realCode;
						
					}
					
					
					$arrBarcodeDes[] = "A0 ".$manuDes." ".$modelDes." ".$subtypeDes." ".$colorDes;
					
				}
				
				$this->view->barcodeCode = $arrBarcodeCode;
				
				$this->view->barcodeDes = $arrBarcodeDes;
				
							
			}
			
			
		}
		//load model After Post 
		$this->view->modelList = $modelList;
		$this->view->number = $number;
		
	}
	
	//Battery Barcode
	/**
	 * Create barcode for battery / power bank
	 * BAT + O/C + Manu - Model - (Optional Special Code) - (Optional Number)
	 * O/C  is  original / compatable 
	 */
	
	public function createBarcodeBatAction(){
		
		$idMainType = 2;
		//Get Sub type 
		$subType = new Model_DbTable_Barcodeproductsubtype();
		//cond
		$cond = new Model_DbTable_Barcode_Condition();
		//manu
		$manu = new Model_DbTable_Barcodemanu();
		//model
		$model = new Model_DbTable_Barcodemodel();
		//get special code 
		$spCode = new Model_DbTable_Barcode_Specialcode();
		//get number 
		$counters = new Model_DbTable_Barcode_Counter();
		
		
		$modelList = array(array('code_model'=>"NA",'des_model'=>"NotAvailable"));
		//$spCodeList = array(array('code_special_code'=>"NA",'des_special_code'=>"NotAvailable")); 

		$number = "N/A";
		
		$arrBarcodeCode = array();
		$arrBarcodeDes = array();
		
		
		
		//Initial Selections 
		$this->view->subtypeList = $subType->listSubTypeByMainTypeID($idMainType);
		$this->view->condList = $cond->listAllConds();
		$this->view->manuList = $manu->listManu();
		$this->view->modelList = $modelList;
		$this->view->spcodeList = $spCode->listSpecialCodeByProductTypeID($idMainType);
		
		//Remember the Choice
		
		$this->view->subtypeChoice = "";
		$this->view->condChoice = "";
		$this->view->manuChoice = "";
		$this->view->spcodeChoice = "";
		$this->view->modelChoice = "";
		//barcode Des
		$this->view->barcodeCode= "";
		$this->view->barcodeDes ="";
		
		if($_POST){
		// will do whatever has been posted
			$modelList = $model->listModelByManuCode(trim($_POST['manu']));

			$this->view->subtypeChoice = trim($_POST['sub_type']);
			$this->view->manuChoice = trim($_POST['manu']);
			$this->view->condChoice = trim($_POST['cond']);			
			$this->view->spcodeChoice = trim($_POST['spcode']);
			
			
		//create Model
			if(isset($_POST['sub_model']) and $_POST['sub_model']=="Model"){
				//do nothing
			}
		//creat Number
			if(isset($_POST['sub_number']) and $_POST['sub_number']=="Number"){
				//if special code not exist , do nothing 
				//else record number 
				$this->view->modelChoice = trim($_POST['model']);
				if(trim($_POST['spcode'])!="NA"){
				
				
				$ccounter = $counters->searchCounterByCodeSpCode(trim($_POST['sub_type']),trim($_POST['spcode']));
				if($ccounter){
					$number=$ccounter['counter']+1;
				}
				else{
					$number=100;
				}
				
				}
				
			}
		//creat result 
			if(isset($_POST['sub_result']) and $_POST['sub_result']=="Result"){
				
				$this->view->modelChoice = trim($_POST['model']);
				$number = trim($_POST['number']);
				//create Barcode
				
				$subtypeDes = $subType->getSubtypeDesByCode(trim($_POST['sub_type']));
				
				$condDes = $cond->getCondDesByCode($_POST['cond']);
				$manuDes = $manu->getManuDesByCode(trim($_POST['manu']));
				$modelDes = $model->getModelDesByCode(trim($_POST['model']));
				$speCodeDes = $spCode->getSpecialDesByCode(trim($_POST['spcode']),$idMainType);
				//var_dump($speCodeDes);
				//put number in it 
				if($_POST['spcode']!="NA" and is_numeric($number)){
					
					$bCodeBeforeTrim = trim($_POST['sub_type']).trim($_POST['cond']).trim($_POST['manu'])."-".trim($_POST['model'])."-".trim($_POST['spcode'])."-".$number;
					$bCodeAfterTrim = $this->trimBarcodeNoColor($bCodeBeforeTrim,trim($_POST['manu']),trim($_POST['model']));
					$arrBarcodeCode[]  = $bCodeAfterTrim;
					$arrBarcodeDes[] = "A0 ".$subtypeDes." ".$condDes." for ".$manuDes." ".$modelDes." ".$speCodeDes." ".$number;
						
				}
				elseif($_POST['spcode']!="NA" and !is_numeric($number)){
					$bCodeBeforeTrim = trim($_POST['sub_type']).trim($_POST['cond']).trim($_POST['manu'])."-".trim($_POST['model'])."-".trim($_POST['spcode']);
					$bCodeAfterTrim = $this->trimBarcodeNoColor($bCodeBeforeTrim,trim($_POST['manu']),trim($_POST['model']));
					$arrBarcodeCode[]  = $bCodeAfterTrim;
					$arrBarcodeDes[] = "A0 ".$subtypeDes." ".$condDes." for ".$manuDes." ".$modelDes." ".$speCodeDes;
						
				}					
				else{
					$bCodeBeforeTrim = trim($_POST['sub_type']).trim($_POST['cond']).trim($_POST['manu'])."-".trim($_POST['model']);
					$bCodeAfterTrim = $this->trimBarcodeNoColor($bCodeBeforeTrim,trim($_POST['manu']),trim($_POST['model']));
					$arrBarcodeCode[]  = $bCodeAfterTrim;
					$arrBarcodeDes[] = "A0 ".$subtypeDes." ".$condDes." for ".$manuDes." ".$modelDes;
				}
				
				$this->view->barcodeCode = $arrBarcodeCode;
				
				$this->view->barcodeDes = $arrBarcodeDes;
								
			}
			
		}
		
		$this->view->modelList = $modelList;
		$this->view->number = $number;
		

	}
	//Charger Barcode
	/**
	 * Create Charge Barcode 
	 */
	public function createBarcodeChargerAction(){
		$idMainType = 3;
		
		//Get Sub type
		$subType = new Model_DbTable_Barcodeproductsubtype();
		//cond
		$cond = new Model_DbTable_Barcode_Condition();
		//manu
		$manu = new Model_DbTable_Barcodemanu();
		//model
		$model = new Model_DbTable_Barcodemodel();
		
		$color = new Model_DbTable_Barcodecolorcodes();
		$counters = new Model_DbTable_Barcode_Counter();
		
		$modelList = array(array('code_model'=>"NA",'des_model'=>"NotAvailable"));
		
		$number = "N/A";
		
		$arrBarcodeCode = array();
		$arrBarcodeDes = array();
		
		//Initial Selections
		
		$this->view->subtypeList = $subType->listSubTypeByMainTypeID($idMainType);
		$this->view->condList = $cond->listAllConds();
		$this->view->manuList = $manu->listManu();
		$this->view->modelList = $modelList;
		$this->view->colorList = $color->listAllColors();

		//Remember the Choice
		
		$this->view->subtypeChoice = "";
		$this->view->condChoice = "";
		$this->view->manuChoice = "";
		$this->view->modelChoice = "";
		$this->view->colorChoice = "";
		
		//barcode Des
		$this->view->barcodeCode= "";
		$this->view->barcodeDes ="";
		
		if($_POST){
			
			$modelList = $model->listModelByManuCode(trim($_POST['manu']));
			$this->view->subtypeChoice = trim($_POST['sub_type']);
			$this->view->condChoice = trim($_POST['cond']);
			$this->view->manuChoice = trim($_POST['manu']);
			$this->view->modelChoice = trim($_POST['model']);
			$number = trim($_POST['number']);
			
			if(isset($_POST['color'])){
				$this->view->colorChoice = $_POST['color'];
			}
			
			//create Model
			if(isset($_POST['sub_model']) and $_POST['sub_model']=="Model"){
				//do nothing
			}
			//create Number
			if(isset($_POST['sub_number']) and $_POST['sub_number']=="Number"){
			
				$ccounter = $counters->searchCounterByCode(trim($_POST['sub_type']),trim($_POST['model']));
				if($ccounter){
					$number=$ccounter['counter']+1;
				}
				else{
					$number=100;
				}
					
					
			}
			
			// Create Barcode
			if(isset($_POST['sub_result']) and $_POST['sub_result']=="Result"){
			

				//create Barcode
			
				$subtypeDes = $subType->getSubtypeDesByCode(trim($_POST['sub_type']));
				$condDes = $cond->getCondDesByCode(trim($_POST['cond']));
				
				$manuDes = $manu->getManuDesByCode(trim($_POST['manu']));
				$modelDes = $model->getModelDesByCode(trim($_POST['model']));
			
				if(isset($_POST['color'])){
				foreach($_POST['color'] as $key => $value){
			
					$colorDes = $color->getColorDes(trim($value));
						
					if(is_numeric($number)){
						$barCodeBeforeTrim = "CH".trim($_POST['sub_type']).trim($_POST['cond']).trim($_POST['manu']).trim($_POST['model'])."-".$number;
						$barCodeAfterTrim = $this->trimBarcodeColor($barCodeBeforeTrim,trim($_POST['manu']),trim($_POST['model']));
						$realCode = '';
						if($barCodeAfterTrim){
							$realCode = $barCodeAfterTrim."-".trim($value);
						}
						$arrBarcodeCode[]  = $realCode;
						$arrBarcodeDes[] = "A0 Charger ".$condDes." ".$subtypeDes." for ".$manuDes." ".$modelDes." ".$colorDes;
							
					}
						
					else{
						
						$barCodeBeforeTrim = "CH".trim($_POST['sub_type']).trim($_POST['cond']).trim($_POST['manu']).trim($_POST['model']);
						$barCodeAfterTrim = $this->trimBarcodeColor($barCodeBeforeTrim,trim($_POST['manu']),trim($_POST['model']));
						$realCode = '';
						if($barCodeAfterTrim){
							$realCode = $barCodeAfterTrim."-".trim($value);
						}
						$arrBarcodeCode[]  = $realCode;
						$arrBarcodeDes[] = "A0 Charger ".$condDes." ".$subtypeDes." for ".$manuDes." ".$modelDes." ".$colorDes;
					}
						
				}
				
				}
				else{
					
					if(is_numeric($number)){
						$barCodeBeforeTrim = "CH".trim($_POST['sub_type']).trim($_POST['cond']).trim($_POST['manu']).trim($_POST['model'])."-".$number;
						$barCodeAfterTrim = $this->trimBarcodeNoColor($barCodeBeforeTrim,trim($_POST['manu']),trim($_POST['model']));
						$realCode = '';
						if($barCodeAfterTrim){
							$realCode = $barCodeAfterTrim."-".trim($value);
						}
						$arrBarcodeCode[]  = $realCode;
						$arrBarcodeDes[] = "A0 Charger ".$condDes." ".$subtypeDes." for ".$manuDes." ".$modelDes;
							
					}
					
					else{
						$barCodeBeforeTrim = "CH".trim($_POST['sub_type']).trim($_POST['cond']).trim($_POST['manu']).trim($_POST['model']);
						$barCodeAfterTrim = $this->trimBarcodeNoColor($barCodeBeforeTrim,trim($_POST['manu']),trim($_POST['model']));
						$realCode = '';
						if($barCodeAfterTrim){
							$realCode = $barCodeAfterTrim."-".trim($value);
						}
						$arrBarcodeCode[]  = $realCode;
						$arrBarcodeDes[] = "A0 Charger ".$condDes." ".$subtypeDes." for ".$manuDes." ".$modelDes;
					}					
					
				}
			
				$this->view->barcodeCode = $arrBarcodeCode;
			
				$this->view->barcodeDes = $arrBarcodeDes;
			
					
			}
			
			
				
			
			
		}
				
		$this->view->modelList = $modelList;
		$this->view->number = $number;
		
		
		
	}
	/**
	 * Create Data cable Barcode 
	 */
	public function createBarcodeDatacableAction(){
		$idMainType = 4;
		$codeSubType = "DC";
		//Get Sub type
		$subType = new Model_DbTable_Barcodeproductsubtype();
		//cond
		$cond = new Model_DbTable_Barcode_Condition();
		//manu
		$manu = new Model_DbTable_Barcodemanu();
		//model
		$model = new Model_DbTable_Barcodemodel();
		
		$color = new Model_DbTable_Barcodecolorcodes();
		$counters = new Model_DbTable_Barcode_Counter();
		
		$modelList = array(array('code_model'=>"NA",'des_model'=>"NotAvailable"));
		
		$number = "N/A";
		
		$arrBarcodeCode = array();
		$arrBarcodeDes = array();
		
		//Initial Selections
			
		$this->view->condList = $cond->listAllConds();
		$this->view->manuList = $manu->listManu();
		$this->view->modelList = $modelList;
		$this->view->colorList = $color->listAllColors();
		
		//Remember the Choice
		
		
		$this->view->condChoice = "";
		$this->view->manuChoice = "";
		$this->view->modelChoice = "";
		$this->view->colorChoice = "";
		$this->view->subtypeChoice = $codeSubType;
		
		//barcode Des
		$this->view->barcodeCode= "";
		$this->view->barcodeDes ="";		
		
		if($_POST){
				
			$modelList = $model->listModelByManuCode(trim($_POST['manu']));
			$this->view->condChoice = trim($_POST['cond']);
			$this->view->manuChoice = trim($_POST['manu']);
			$this->view->modelChoice = trim($_POST['model']);
			$number = trim($_POST['number']);
			
			if(isset($_POST['color'])){
				$this->view->colorChoice = $_POST['color'];
			}
				
			//create Model
			if(isset($_POST['sub_model']) and $_POST['sub_model']=="Model"){
				//do nothing
			}			
			
			//create Number
			if(isset($_POST['sub_number']) and $_POST['sub_number']=="Number"){
					
				$ccounter = $counters->searchCounterByCode($codeSubType,trim($_POST['model']));
				if($ccounter){
					$number=$ccounter['counter']+1;
				}
				else{
					$number=100;
				}
					
					
			}
			// Create Barcode
			if(isset($_POST['sub_result']) and $_POST['sub_result']=="Result"){
					
			
				//create Barcode
					
				$condDes = $cond->getCondDesByCode(trim($_POST['cond']));
			
				$manuDes = $manu->getManuDesByCode(trim($_POST['manu']));
				$modelDes = $model->getModelDesByCode(trim($_POST['model']));
					
				foreach($_POST['color'] as $key => $value){
						
					$colorDes = $color->getColorDes(trim($value));
			
					if(is_numeric($number)){
						$barCodeBeforeTrim ="DC".trim($_POST['cond']).trim($_POST['manu']).trim($_POST['model'])."-".$number;
						$barCodeAfterTrim = $this->trimBarcodeColor($barCodeBeforeTrim,trim($_POST['manu']),trim($_POST['model']));
						$realCode = '';
						if($barCodeAfterTrim){
							$realCode = $barCodeAfterTrim."-".trim($value);
						} 
						$arrBarcodeCode[]  = $realCode;
						$arrBarcodeDes[] = "A0 Data Cable ".$condDes." for ".$manuDes." ".$modelDes." ".$colorDes;
							
					}
			
					else{
						$barCodeBeforeTrim ="DC".trim($_POST['cond']).trim($_POST['manu']).trim($_POST['model']);
						$barCodeAfterTrim = $this->trimBarcodeColor($barCodeBeforeTrim,trim($_POST['manu']),trim($_POST['model']));
						$realCode = '';
						if($barCodeAfterTrim){
							$realCode = $barCodeAfterTrim."-".trim($value);
						}
						$arrBarcodeCode[]  = $realCode;
						$arrBarcodeDes[] = "A0 Data Cable ".$condDes." for ".$manuDes." ".$modelDes." ".$colorDes;
					}
			
				}
					
				$this->view->barcodeCode = $arrBarcodeCode;
					
				$this->view->barcodeDes = $arrBarcodeDes;
					
					
			}
		
		}	
		
		
		$this->view->modelList = $modelList;
		$this->view->number = $number;
		
	}
	/**
	 * Create handsfree headset barcode
	 */
	public function createBarcodeHandsfreeAction(){
		$idMainType = 5;
		$codeSubType = "HF";
		//Get Sub type
		$subType = new Model_DbTable_Barcodeproductsubtype();
		//cond
		$cond = new Model_DbTable_Barcode_Condition();
		//manu
		$manu = new Model_DbTable_Barcodemanu();
		//model
		$model = new Model_DbTable_Barcodemodel();
		
		$color = new Model_DbTable_Barcodecolorcodes();
		$counters = new Model_DbTable_Barcode_Counter();
		
		$modelList = array(array('code_model'=>"NA",'des_model'=>"NotAvailable"));
		
		$number = "N/A";
		
		$arrBarcodeCode = array();
		$arrBarcodeDes = array();
		
		//Initial Selections
			
		$this->view->condList = $cond->listAllConds();
		$this->view->manuList = $manu->listManu();
		$this->view->modelList = $modelList;
		$this->view->colorList = $color->listAllColors();
		
		//Remember the Choice
		
		
		$this->view->condChoice = "";
		$this->view->manuChoice = "";
		$this->view->modelChoice = "";
		$this->view->colorChoice = "";
		$this->view->subtypeChoice = $codeSubType;
		
		//barcode Des
		$this->view->barcodeCode= "";
		$this->view->barcodeDes ="";
		
		if($_POST){
		
			$modelList = $model->listModelByManuCode(trim($_POST['manu']));
			$this->view->condChoice = trim($_POST['cond']);
			$this->view->manuChoice = trim($_POST['manu']);
			$this->view->modelChoice = trim($_POST['model']);
			$number = trim($_POST['number']);
				
			if(isset($_POST['color'])){
				$this->view->colorChoice = $_POST['color'];
			}
		
			//create Model
			if(isset($_POST['sub_model']) and $_POST['sub_model']=="Model"){
				//do nothing
			}
				
			//create Number
			if(isset($_POST['sub_number']) and $_POST['sub_number']=="Number"){
					
				$ccounter = $counters->searchCounterByCode($codeSubType,trim($_POST['model']));
				if($ccounter){
					$number=$ccounter['counter']+1;
				}
				else{
					$number=100;
				}
					
					
			}
			// Create Barcode
			if(isset($_POST['sub_result']) and $_POST['sub_result']=="Result"){
					
					
				//create Barcode
					
				$condDes = $cond->getCondDesByCode(trim($_POST['cond']));
					
				$manuDes = $manu->getManuDesByCode(trim($_POST['manu']));
				$modelDes = $model->getModelDesByCode(trim($_POST['model']));
					
				foreach($_POST['color'] as $key => $value){
		
					$colorDes = $color->getColorDes(trim($value));
						
					if(is_numeric($number)){
						$barCodeBeforeTrim = "HF".trim($_POST['cond']).trim($_POST['manu']).trim($_POST['model'])."-".$number;
						$barCodeAfterTrim = $this->trimBarcodeColor($barCodeBeforeTrim,trim($_POST['manu']),trim($_POST['model']));
						$realCode = '';
						if($barCodeAfterTrim){
							$realCode = $barCodeAfterTrim."-".trim($value);	
						}
						$arrBarcodeCode[]  = $realCode;							
					}
						
					else{
						$barCodeBeforeTrim = "HF".trim($_POST['cond']).trim($_POST['manu']).trim($_POST['model']);
						$barCodeAfterTrim = $this->trimBarcodeColor($barCodeBeforeTrim,trim($_POST['manu']),trim($_POST['model']));
						$realCode = '';
						if($barCodeAfterTrim){
							$realCode = $barCodeAfterTrim."-".trim($value);
						}
						$arrBarcodeCode[]  = $realCode;
						

					}
					
					$arrBarcodeDes[] = "A0 Hands Free ".$condDes." for ".$manuDes." ".$modelDes." ".$colorDes;						
				}
					
				$this->view->barcodeCode = $arrBarcodeCode;
					
				$this->view->barcodeDes = $arrBarcodeDes;
					
					
			}
		
		}
		
		
		$this->view->modelList = $modelList;
		$this->view->number = $number;
				
	}
	
	/**
	 * Create Holder barcode
	 */
	public function createBarcodeHolderAction(){
		$idMainType = 6;
		//$codeSubType = "HF";
		//Get Sub type
		$subType = new Model_DbTable_Barcodeproductsubtype();
		//spcode 
		$spCode = new Model_DbTable_Barcode_Specialcode();
		// Number 
		$counters = new Model_DbTable_Barcode_Counter();
		
		$this->view->spcodeList = $spCode->listSpecialCodeByProductTypeID($idMainType);
		$this->view->subtypeList = $subType->listSubTypeByMainTypeID($idMainType);
		//Remember the Choice
		
		$this->view->subtypeChoice = "";

		$this->view->spcodeChoice = "";

		$number = "N/A";
		
		$arrBarcodeCode = array();
		$arrBarcodeDes = array();

		//Remember the Choice
		
		
		
		//barcode Des
		$this->view->barcodeCode= "";
		$this->view->barcodeDes ="";
		
		if($_POST){
			
			$this->view->subtypeChoice = trim($_POST['sub_type']);
			$this->view->spcodeChoice = trim($_POST['spcode']);
			
			$number = trim($_POST['number']);
		
			//create Number
					if(isset($_POST['sub_number']) and $_POST['sub_number']=="Number"){
				//if special code not exist , do nothing 

				if(trim($_POST['spcode'])!="NA"){
				
				
				$ccounter = $counters->searchCounterByCodeSpCode(trim($_POST['sub_type']),trim($_POST['spcode']));
				if($ccounter){
					$number=$ccounter['counter']+1;
				}
				else{
					$number=100;
				}
				
				}
				
			}
				//creat result 
			if(isset($_POST['sub_result']) and $_POST['sub_result']=="Result"){
				
				//create Barcode
				
				$subtypeDes = $subType->getSubtypeDesByCode(trim($_POST['sub_type']));
				$speCodeDes = $spCode->getSpecialDesByCode(trim($_POST['spcode']),$idMainType);

				
				if($_POST['spcode']!="NA" and is_numeric($number)){
					$arrBarcodeCode[]  = trim($_POST['sub_type'])."-".trim($_POST['spcode'])."-".$number;
					$arrBarcodeDes[] = "A0 ".$subtypeDes." ".$speCodeDes." ".$number;
						
				}
				elseif($_POST['spcode']!="NA" and !is_numeric($number)){
					$arrBarcodeCode[]  = trim($_POST['sub_type'])."-".trim($_POST['spcode']);
					$arrBarcodeDes[] =  "A0 ".$subtypeDes." ".$speCodeDes;
						
				}					
				else{
						//doing nothing 
				}
				
				$this->view->barcodeCode = $arrBarcodeCode;
				
				$this->view->barcodeDes = $arrBarcodeDes;
								
			}
		
		}
		
		
		$this->view->number = $number;
					
		
	}
	/**
	 * Create Screen Protector barcode
	 */
	public function createBarcodeSpAction(){
		$idMainType = 7;
		
		//Get Sub type
		$subType = new Model_DbTable_Barcodeproductsubtype();
		//cond
		//$cond = new Model_DbTable_Barcode_Condition();
		//manu
		$manu = new Model_DbTable_Barcodemanu();
		//model
		$model = new Model_DbTable_Barcodemodel();
		
		$color = new Model_DbTable_Barcodecolorcodes();
		$counters = new Model_DbTable_Barcode_Counter();
		
		$modelList = array(array('code_model'=>"NA",'des_model'=>"NotAvailable"));
		
		$number = "N/A";
		
		$arrBarcodeCode = array();
		$arrBarcodeDes = array();
		
		//Initial Selections
		
		$this->view->subtypeList = $subType->listSubTypeByMainTypeID($idMainType);
		//$this->view->condList = $cond->listAllConds();
		$this->view->manuList = $manu->listManu();
		$this->view->modelList = $modelList;
		$this->view->colorList = $color->listAllColors();
		
		//Remember the Choice
		
		$this->view->subtypeChoice = "";
		//$this->view->condChoice = "";
		$this->view->manuChoice = "";
		$this->view->modelChoice = "";
		$this->view->colorChoice = "";
		
		//barcode Des
		$this->view->barcodeCode= "";
		$this->view->barcodeDes ="";
		
		if($_POST){
				
			$modelList = $model->listModelByManuCode(trim($_POST['manu']));
			$this->view->subtypeChoice = trim($_POST['sub_type']);
			//$this->view->condChoice = trim($_POST['cond']);
			$this->view->manuChoice = trim($_POST['manu']);
			$this->view->modelChoice = trim($_POST['model']);
			$number = trim($_POST['number']);
				
			if(isset($_POST['color'])){
				$this->view->colorChoice = $_POST['color'];
			}
				
			//create Model
			if(isset($_POST['sub_model']) and $_POST['sub_model']=="Model"){
				//do nothing
			}
			//create Number
			if(isset($_POST['sub_number']) and $_POST['sub_number']=="Number"){
					
				$ccounter = $counters->searchCounterByCode(trim($_POST['sub_type']),trim($_POST['model']));
				if($ccounter){
					$number=$ccounter['counter']+1;
				}
				else{
					$number=100;
				}
					
					
			}
				
			// Create Barcode
			if(isset($_POST['sub_result']) and $_POST['sub_result']=="Result"){
					
		
				//create Barcode
					
				$subtypeDes = $subType->getSubtypeDesByCode(trim($_POST['sub_type']));
				//$condDes = $cond->getCondDesByCode(trim($_POST['cond']));
		
				$manuDes = $manu->getManuDesByCode(trim($_POST['manu']));
				$modelDes = $model->getModelDesByCode(trim($_POST['model']));

				if(isset($_POST['color'])){
				foreach($_POST['color'] as $key => $value){
						
					$colorDes = $color->getColorDes(trim($value));
		
					if(is_numeric($number)){
						$barCodeBeforeTrim = "SP".trim($_POST['sub_type']).trim($_POST['manu']).trim($_POST['model'])."-".$number;
						$barCodeAfterTrim = $this->trimBarcodeColor($barCodeBeforeTrim,trim($_POST['manu']),trim($_POST['model']));
						$realCode = '';
						if($barCodeAfterTrim){
							$realCode = $barCodeAfterTrim."-".trim($value);
						}
						$arrBarcodeCode[]  = $realCode;
					}
		
					else{
						$barCodeBeforeTrim = "SP".trim($_POST['sub_type']).trim($_POST['manu']).trim($_POST['model']);
						$barCodeAfterTrim = $this->trimBarcodeColor($barCodeBeforeTrim,trim($_POST['manu']),trim($_POST['model']));
						$realCode = '';
						if($barCodeAfterTrim){
							$realCode = $barCodeAfterTrim."-".trim($value);
						}
						$arrBarcodeCode[]  = $realCode;

					}
						$arrBarcodeDes[] = "A0 Screen Protector ".$subtypeDes." for ".$manuDes." ".$modelDes." ".$colorDes;		
				}
				}
				else{
					
					if(is_numeric($number)){
						$barCodeBeforeTrim = "SP".trim($_POST['sub_type']).trim($_POST['manu']).trim($_POST['model'])."-".$number;
						$barCodeAfterTrim = $this->trimBarcodeNoColor($barCodeBeforeTrim,trim($_POST['manu']),trim($_POST['model']));
						$realCode = '';
						if($barCodeAfterTrim){
							$realCode = $barCodeAfterTrim."-".trim($value);
						}
						$arrBarcodeCode[]  = $realCode;						
					}
					
					else{
						$barCodeBeforeTrim = "SP".trim($_POST['sub_type']).trim($_POST['manu']).trim($_POST['model']);
						$barCodeAfterTrim = $this->trimBarcodeNoColor($barCodeBeforeTrim,trim($_POST['manu']),trim($_POST['model']));
						$realCode = '';
						if($barCodeAfterTrim){
							$realCode = $barCodeAfterTrim."-".trim($value);
						}
						$arrBarcodeCode[]  = $realCode;
						
					}					
					$arrBarcodeDes[] = "A0 Screen Protector ".$subtypeDes." for ".$manuDes." ".$modelDes;					
					
				}
				
				
					
				$this->view->barcodeCode = $arrBarcodeCode;
					
				$this->view->barcodeDes = $arrBarcodeDes;
					
					
			}
				
				
		
				
				
		}
		
		$this->view->modelList = $modelList;
		$this->view->number = $number;
		
		
				
	}
	/**
	 * Other barcode include subtype like 
	 * Bag / Fmtransmiter etc
	 * 
	 */
	public function createBarcodeOtherAction(){
		$idMainType = 8;
		//Get Sub type
		$subType = new Model_DbTable_Barcodeproductsubtype();

		//get special code
		$spCode = new Model_DbTable_Barcode_Specialcode();
		//get number
		$color = new Model_DbTable_Barcodecolorcodes();
		$counters = new Model_DbTable_Barcode_Counter();
		
		
		//$spCodeList = array(array('code_special_code'=>"NA",'des_special_code'=>"NotAvailable"));
		
		$number = "N/A";
		
		$arrBarcodeCode = array();
		$arrBarcodeDes = array();
		
		
		
		//Initial Selections
		$this->view->subtypeList = $subType->listSubTypeByMainTypeID($idMainType);
		$this->view->spcodeList = $spCode->listSpecialCodeByProductTypeID($idMainType);
		$this->view->colorList = $color->listAllColors();
		
		//Remember the Choice
		
		$this->view->subtypeChoice = "";
		$this->view->colorChoice = "";
		$this->view->spcodeChoice = "";

		//barcode Des
		$this->view->barcodeCode= "";
		$this->view->barcodeDes ="";
		
		if($_POST){

			
			$this->view->subtypeChoice = trim($_POST['sub_type']);
			$this->view->spcodeChoice = trim($_POST['spcode']);
			$number = trim($_POST['number']);
			
			if(isset($_POST['color'])){
				$this->view->colorChoice = $_POST['color'];
			}
			
			//creat Number
			if(isset($_POST['sub_number']) and $_POST['sub_number']=="Number"){
				//if special code not exist , do nothing
				if(trim($_POST['spcode'])!="NA"){
		
		
					$ccounter = $counters->searchCounterByCodeSpCode(trim($_POST['sub_type']),trim($_POST['spcode']));
				
					if($ccounter){
						$number=$ccounter['counter']+1;
						$counters->updateCounter($ccounter['id_counter'], $number);
					}
					else{
						$number=100;
						$counters->addCounter(trim($_POST['sub_type']), trim($_POST['spcode']), 100);
					}
		
				}
		
			}
			//creat result
			
			
			
			if(isset($_POST['sub_result']) and $_POST['sub_result']=="Result"){
				//create Barcode
		
			
				$subtypeDes = $subType->getSubtypeDesByCode(trim($_POST['sub_type']));
				$speCodeDes = $spCode->getSpecialDesByCode(trim($_POST['spcode']),$idMainType);
			
				
				//var_dump($speCodeDes);
				//put number in it
				
				if(isset($_POST['color'])){
				foreach($_POST['color'] as $key => $value){
			
					$colorDes = $color->getColorDes(trim($value));
						
					if(is_numeric($number)){
						$barCodeBeforeTrim = "OTH-".trim($_POST['sub_type']).trim($_POST['spcode'])."-".$number;
						$barCodeAfterTrim = $this->trimBarcodeColor($barCodeBeforeTrim,'APPLE',"IPH6P"); // can only do the - remove
						$realCode = '';
						
						if($barCodeAfterTrim){
							$realCode = $barCodeAfterTrim."-".trim($value);	
						}
						$arrBarcodeCode[]  = $realCode; 
						
						$arrBarcodeDes[] = "A0 Other ".$subtypeDes." ".$speCodeDes." ".$number." ".$colorDes;
							
					}
						
					else{
						
						$barCodeBeforeTrim = "OTH-".trim($_POST['sub_type']).trim($_POST['spcode']);
						$barCodeAfterTrim = $this->trimBarcodeColor($barCodeBeforeTrim,'APPLE',"IPH6P"); // can only do the - remove
						$realCode = '';
						
						if($barCodeAfterTrim){
							$realCode = $barCodeAfterTrim."-".trim($value);	
						}
						$arrBarcodeCode[]  = $realCode; 
						
						
						$arrBarcodeDes[] = "A0 Other ".$subtypeDes." ".$speCodeDes." ".$colorDes;
					}
						
				}
				
				}
				else{
					
					if(is_numeric($number)){
						$arrBarcodeCode[]  ="OTH-".trim($_POST['sub_type']).trim($_POST['spcode'])."-".$number;
						$arrBarcodeDes[] =  "A0 Other ".$subtypeDes." ".$speCodeDes." ".$number;
							
					}
					
					else{
						$arrBarcodeCode[]  ="OTH-".trim($_POST['sub_type']).trim($_POST['spcode']);
						$arrBarcodeDes[] = "A0 Other ".$subtypeDes." ".$speCodeDes;
					}					
					
				}
			
				$this->view->barcodeCode = $arrBarcodeCode;
			
				$this->view->barcodeDes = $arrBarcodeDes;
			
					
				
		}
		

	}
	
	$this->view->number = $number;
	}
	/**
	 * Create barcode stylus pen
	 */
	public function createBarcodeStylusAction(){
		
		$idMainType = 9;
		//Get Sub type
		$subType = new Model_DbTable_Barcodeproductsubtype();
		$color = new Model_DbTable_Barcodecolorcodes();
		$counters = new Model_DbTable_Barcode_Counter();
		
		
		$number = "N/A";
		
		$arrBarcodeCode = array();
		$arrBarcodeDes = array();
		
		
		
		//Initial Selections
		$this->view->subtypeList = $subType->listSubTypeByMainTypeID($idMainType);

		$this->view->colorList = $color->listAllColors();
		
		

		//Remember the Choice
		
		$this->view->subtypeChoice = "";
		$this->view->colorChoice = "";
		$this->view->spcodeChoice = "NA";
		
		//barcode Des
		$this->view->barcodeCode= "";
		$this->view->barcodeDes ="";
		
		if($_POST){
			$this->view->subtypeChoice = trim($_POST['sub_type']);
			
			$number = trim($_POST['number']);
			
			if(isset($_POST['sub_number']) and $_POST['sub_number']=="Number"){
				
				
				
				$idSubType = $subType->getSubtypeIDByCode(trim($_POST['sub_type']));
				
				$ccounter = $counters->searchCounter($idSubType,7); // default NA special code is 7
				if($ccounter){
					$number=$ccounter['counter']+1;
				}
				else{
					$number=100;
				}
				
			}
			
			// generate Result 
			if(isset($_POST['sub_result']) and $_POST['sub_result']=="Result"){

				$subtypeDes = $subType->getSubtypeDesByCode(trim($_POST['sub_type']));
				
				
				
				if(isset($_POST['color'])){
					foreach($_POST['color'] as $key => $value){
							
						$colorDes = $color->getColorDes(trim($value));
				
						if(is_numeric($number)){
							$arrBarcodeCode[]  =trim($_POST['sub_type'])."-".$number."-".trim($value);
							$arrBarcodeDes[] = "A0 ".$subtypeDes." ".$number." ".$colorDes;
								
						}
				
						else{
							$arrBarcodeCode[]  =trim($_POST['sub_type'])."-".trim($value);
							$arrBarcodeDes[] = "A0 ".$subtypeDes." ".$colorDes;
						}
				
					}
				
				}
				else{
						
					if(is_numeric($number)){
							$arrBarcodeCode[]  =trim($_POST['sub_type'])."-".$number;
							$arrBarcodeDes[] = "A0 ".$subtypeDes." ".$number;
							
					}
						
					else{
						$arrBarcodeCode[]  ="ERRORERROR";
						$arrBarcodeDes[] = "Can Not Create Barcode , Error Occur, Do Not Proceed";
					}
						
				}
					
				$this->view->barcodeCode = $arrBarcodeCode;
					
				$this->view->barcodeDes = $arrBarcodeDes;
				
				
				
				
				
			}
		}
		
		$this->view->number = $number;
		
	}
	/**
	 * Add barcode more color function solve a problem that if the user just create bacode for 
	 * some of the color at the first place, then since the item sales good , the warehouse want more color 
	 * for the barcode , instead of manual add , using this function to add color and generate similar
	 * apos description for the item 
	 */
	public function addBarcodeMoreColorAction(){
		
		$color = new Model_DbTable_Barcodecolorcodes();
		$pro = new Model_DbTable_Productsva();
		
		$this->view->cList = $color->listAllColors();
		
		
		//d($this->view->cList);
		$arrCode = array();
		$arrDes = array();
		$arrChoice = array();
		
		if($_POST){
			
			if(isset($_POST['btn_gen'])){
				
				$barcode = strtoupper(trim($_POST['barcode']));
					
				$des = Model_System_Downloadhelper::downloadUrl("http://".SERVER_IP_COMM."/products/get-old-apos-product-description/barcode/".$barcode);
				$positionDash = strrpos($barcode,"-");
				$shortBarcode = $barcode;
				if($positionDash > 0){
					$shortBarcode = substr($barcode,0,$positionDash);
					$idColor = substr($barcode,$positionDash);
					// find description , and according the color code , find color description , replace descrtion
					$desColor = $color->getColorDes($idColor);
					
					if(isset($_POST['no_color'])){
						
						$shortBarcode = $barcode;
					}
					
					
					foreach($_POST['color'] as $colorID){
						$barcodeNew = $shortBarcode.'-'.$colorID;
							
						$arrCode[$colorID] = $barcodeNew;
							
						$desColorNew = $color->getColorDes($colorID);
							
						if(trim($desColor)!=""){
							$desNew = str_replace($desColor, $desColorNew, $des);
						}
						else{
							$desNew = $des." ".$desColorNew;
						}
						$arrDes[$colorID] = $desNew;
						// check if the barcode exist
						$arrChoice[$colorID] = $pro->ifExist($barcodeNew);
					}
					//foreach()
					// find the descrition is in the list , do not need any more , disable it
				
					// save barcode
				
				
				
				}else{
					// will not do nothing
					echo "Can Not Find any -, can not add any color";
				
				}
					
				// find all colors
				//generate
					
				$this->view->arrCode = $arrCode;
				$this->view->arrDes = $arrDes;
				$this->view->arrChoice = $arrChoice;
				
			}
			
			//remove dash and get short code 
			
			if(isset($_POST['btn_save'])){

				foreach($_POST['choose'] as $k => $v){
					$pro->addProduct($_POST['barcode'][$k], $_POST['des'][$k],$_POST['country']);
				}
	
			}
		}
		
		
		
	}
	
	/**
	 * All Create Barcode functon will submit result to savebarcode function 
	 * the barcode will be saved and the counter for substyle will be recorded 
	 * 
	 */
	
	public function saveBarcodeAction(){
		
		$subType = new Model_DbTable_Barcodeproductsubtype();
		$products = new Model_DbTable_Productsva();
		$counter = new Model_DbTable_Barcode_Counter();
		$model = new Model_DbTable_Barcodemodel();
		$spCode = new Model_DbTable_Barcode_Specialcode();
		
		
		if($_POST){
		
		//save in barcode into database 
		
		
		foreach($_POST['barcode'] as $key => $value){
			
			$products->addProduct($value, $_POST['title'][$key],$_POST['country']);		
		}
		
		//Save Counter 
		if(isset($_POST['number_counter']) && $_POST['number_counter']!="N/A"){
				$idSubtype = $subType->getSubtypeIDByCode($_POST['code_sub_type']);
				$idMainType = $subType->getMainTypeIDByCode($_POST['code_sub_type']);
				if(isset($_POST['code_model'])){
				$idModel = $model->getModelIDByCode($_POST['code_model']);
			
			if(intval($_POST['number_counter']) ==100){
								
				$counter->addCounter($idSubtype, $idModel, 100);
			}
			else{
				
				$counter->runCounter($idSubtype, $idModel,$_POST['number_counter']);
			}
				}
				if(isset($_POST['code_spcode'])){
					
				$idSpCode = $spCode->getSpecialByCode($_POST['code_spcode'], $idMainType);
				
				if(intval($_POST['number_counter']) ==100){
				
					$counter->addCounter($idSubtype, $idSpCode['id_special'], 100);
				}
				else{
				
					$counter->runCounter($idSubtype,$idSpCode['id_special'],$_POST['number_counter']);
				}					
					
				}
				
		}
		
		//save into product 

		//	
			
			
		echo "BarCode and Description Saved, You may now Close this page";	
		
		}
		else{
			
		echo "You should Not Directly Visit this page";
		}
		
	}
	
	/**
	 * This is assist function the barcode length for apos is 16 chr 
	 * if the barcode generator create more then 16 char barcode 
	 * this funcion will using for trim the '-' to fit the barcode in to 16 chr
	 * This function will help to trim the barcode with color code  
	 * @param unknown_type $barcode
	 * @param unknown_type $manuCode
	 * @param unknown_type $modelCode
	 */
	
	public function trimBarcodeColor($barcode,$manuCode,$modelCode){
		// trim it to 13 digi
		$bCode = $barcode;
		$bModel = new Model_DbTable_Barcodemodel();
		$replaceCode = $bModel->replaceShortCode($modelCode);
		$barCodeResult = '';
		while(strlen($bCode) > 13 && $barCodeResult != $bCode){
			$barCodeResult = $bCode;
			//step 1
			d($barCodeResult,$bCode);
			$bCode =  preg_replace('/\-/', '', $bCode, 1);
			//step 2, do when the first step is not working
			if($bCode == $barCodeResult){
				// if manufacture code only appear once you can remove it or you can NOT
				if(substr_count($bCode,$manuCode) == 1){
					$bCode = str_replace($manuCode, '', $bCode);
					d("I am in Step 2",$manuCode);
				}
				else{
					//step 3
	
					if(substr_count($bCode,$modelCode) == 1){
						d("I am in Step 3",$replaceCode);
						$bCode = str_replace($modelCode,$replaceCode,$bCode);
					}
				}
					
			}
				
				
				
				
		}
	
		if(strlen($bCode)>13){
				
			return false;
	
		}else{
				
			return $bCode;
		}
	
	
	}
	/**
	 * This is simiar function to trim the barcode , however , this is for the barcode 
	 * which do NOT have color code 
	 * 
	 * @param unknown_type $barcode
	 * @param unknown_type $manuCode
	 * @param unknown_type $modelCode
	 * @return boolean|mixed
	 */
	public function trimBarcodeNoColor($barcode,$manuCode,$modelCode){
	
		$bCode = $barcode;
		$bModel = new Model_DbTable_Barcodemodel();
		$replaceCode = $bModel->replaceShortCode($modelCode);
		$barCodeResult = '';
	
		while(strlen($bCode) > 16 && $barCodeResult!= $bCode){
				
			//d($barCodeResult,$bCode);
	
			$barCodeResult = $bCode;
			//step 1
			$bCode =  preg_replace('/\-/', '', $bCode, 1);
			//step 2, do when the first step is not working
			if($bCode == $barCodeResult){
				// if manufacture code only appear once you can remove it or you can NOT
				if(substr_count($bCode,$manuCode) == 1){
					d("I am in Step 2",$manuCode);
					$bCode = str_replace($manuCode, '', $bCode);
					d($bCode);
				}
				else{
					//step 3
	
					if( (substr_count($bCode,$modelCode) == 1) && $replaceCode!=''){
						d("I am in Step 3",$replaceCode);
						$bCode = str_replace($modelCode,$replaceCode,$bCode);
					}
				}
					
			}
				
				
				
				
		}
	
		if(strlen($bCode) > 16){
				
			return false;
	
		}else{
				
			return $bCode;
		}
	
	
	
	}
	
	//------------------------------------End of Create Barcode Secton  ----------------------------------------------------//
	
	//WIll Barcode Function move to Repair Parts
	
	
	//------------------------------------Begin of Create File Section  ----------------------------------------------------//
	/**
	 * the user can select day to export the barcode created today 
	 * the export file will be in CSV format 
	 * then using this csv file , user can import the barcode created in APOS
	 * 
	 */
	public function createFileAction(){
		$folder = getcwd()."/barcoderesult2/";
		
		
		$dateToday = Model_DatetimeHelper::dateToday();
		$db = $this->_getParam("date");
		if(trim($db) != ''){
			
			$dateToday = $db;
		}
		
		$fileName ="barcode-".$dateToday.".csv";
		
		$products = new Model_DbTable_Productsva();
		$pList = $products->listProductsByData($dateToday);
		if($pList){
			
		$fl = fopen($folder.$fileName,"a");
			
			foreach($pList as $key => $value){
								
				fputcsv($fl, $value);
			}
		fclose($fl);	
		} 
		
		
	}
	/**
	 * This funcion is create for China GUAN JIA PO 
	 * it will export guan jia po Excel file format for china department import 
	 */
	public function createGjpFileAction(){
		
		$folder = getcwd()."/gjpresult/";
		$this->view->date = Model_DatetimeHelper::dateToday();
		
		if($_POST){
			
		
		
		$dateCheck = $_POST['date'];
		
		$fileName ="gjp-".$dateCheck.".xls";
		
		$products = new Model_DbTable_Productsva();
		
		@unlink($folder.$fileName);
		
		
		$pList = $products->listProductsByData($dateCheck);
		
		$arrRes = array();
		
		if($pList){
			foreach($pList as $pRow){
				
				$arrRes[] = array($pRow['code_product'],$pRow['title_product'],$pRow['country']);
			}		
			$fl = new Model_Fileshandler();
			$fl->exportGjpExcel($arrRes, $folder.$fileName);
		}
		
		}
		
	}
	//------------------------------------End of Create File Secton  ----------------------------------------------------//
	/**
	 * All barcode controller function is sorted and explained on 2015-02-14
	 */
}
?>